package org.mq.mymq.nameserver;

import io.netty.util.internal.StringUtil;
import org.mq.mymq.nameserver.common.CommonCache;
import org.mq.mymq.nameserver.common.TraceReplicationProperties;
import org.mq.mymq.nameserver.core.InvalidServiceRemoveTask;
import org.mq.mymq.nameserver.core.NameServerStarter;
import org.mq.mymq.nameserver.enums.ReplicationModeEnum;
import org.mq.mymq.nameserver.enums.ReplicationRoleEnum;
import org.mq.mymq.nameserver.replication.*;

import java.io.IOException;

/**
 * @ClassName NameServerStartUp
 * @Description 注册中心启动类
 * @Author jiarong_ye
 * @Date 2025/6/18 18:56
 * @Version 1.0
 */
public class NameServerStartUp {
    private static NameServerStarter starter;
    private static ReplicationService replicationService = new ReplicationService();

    /**
     * 分片复制逻辑初始化
     */
    private static void initReplicationService() {
        // 参数校验
        ReplicationModeEnum replicationModeEnum = replicationService.checkProperties();
        // 开始同步任务
        replicationService.startReplicationTask(replicationModeEnum);
        ReplicationTask replicationTask = null;
        if (replicationModeEnum == ReplicationModeEnum.MASTER_SLAVE) {
            ReplicationRoleEnum replicationRoleEnum = ReplicationRoleEnum.of(CommonCache.getNameserverProperties().getMasterSlaveReplicationProperties().getRole());
            if (replicationRoleEnum == ReplicationRoleEnum.MASTER) {
                // 主从复制模式下主节点开启数据发送从节点任务
                replicationTask = new MasterReplicationMsgSendTask("master-replication-msg-send-task");
                replicationTask.startTaskAsync();
            } else if (replicationRoleEnum == ReplicationRoleEnum.SLAVE) {
                // 主从模式下从节点开启心跳任务发送给主节点
                replicationTask = new SlaveReplicationHeartBeatTask("slave-replication-heartbeat-task");
                replicationTask.startTaskAsync();
            }
            CommonCache.setReplicationTask(replicationTask);
        } else if (replicationModeEnum == ReplicationModeEnum.TRACE) {
            // 当前不是尾节点，开启一个复制数据的异步任务
            TraceReplicationProperties traceReplicationProperties = CommonCache.getNameserverProperties().getTraceReplicationProperties();
            if (!StringUtil.isNullOrEmpty(traceReplicationProperties.getNextNode())) {
                replicationTask = new NodeReplicationSendMsgTask("node-replication-msg-send-task");
                replicationTask.startTaskAsync();
            }
        }
    }

    private static void initInvalidServerRemoveTask() {
        Thread invalidServiceRemoveTask = new Thread(new InvalidServiceRemoveTask());
        invalidServiceRemoveTask.setName("invalid-server-remove-task");
        invalidServiceRemoveTask.start();
    }

    public static void main(String[] args) throws InterruptedException, IOException {
        // 获取到集群复制的配置属性
        // master-slave复制？trace？
        // 如果是主从复制->开启一个额外的netty进程->slave链接接入->当数据写入master节点的时候，把写入数据同步到slave节点
        // 如果是链路复制->开启一个额外的netty进程->slave端去链接master节点
        CommonCache.getPropertiesLoader().loadProperties();
        initReplicationService();
        initInvalidServerRemoveTask();
        starter = new NameServerStarter(CommonCache.getNameserverProperties().getNameserverPort());
        starter.startServer();
    }
}
